﻿using System.Reflection;

namespace DiffuseEnjoyIdle.Core.Helper
{
    public static class EditHelper
    {

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="Dto"></param>
        /// <param name="Model"></param>
        /// <param name="isCoverOriginalValue"></param>
        /// <param name="exclude"></param>
        public static void Edit<S, T>(this S Dto, T Model, params string[] exclude)
        {
            Type sourceType = Dto.GetType();//获得类型  
            Type targetType = typeof(T);
            object oldvalue;
            object newvalue;
            var sps = sourceType.GetProperties();
            foreach (PropertyInfo dp in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                //如果有需要排除的字段
                if (exclude != null && exclude.Contains(dp.Name))
                    continue;

                foreach (PropertyInfo sp in sps)//获得类型的属性字段  
                {
                    if (sp.Name == dp.Name)//判断属性名是否相同  
                    {
                        oldvalue = dp.GetValue(Model, null);
                        newvalue = sp.GetValue(Dto, null);

                        if (oldvalue == newvalue) break;
                        dp.SetValue(Model, sp.GetValue(Dto, null), null);//获得s对象属性的值复制给d对象的属性  

                    }
                }
            }
        }

    }
}
